package org.yinxianren.www.study_02.study.httpTest;

import org.junit.Test;
import org.springframework.util.Assert;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.*;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class PayTestTwo {

    @Test
    public void test_01(){
        String str=String.format("我今年【%s】岁,%s是好天气",18,"7月16日");

        System.out.println(str);
    }






    private static Map<String, Lock>  merchantsLock= Collections.synchronizedMap(new HashMap<>());


//    public static void main(String[] args) {
//        Lock lockM201001=getLock("M201001");
//        Lock lockM201002=getLock("M201002");
//        Lock lockM201003=getLock("M201003");
//
//        for(int i=0;i<10;i++){
//           new Thread(()->lock01(lockM201001)) .start();
//            new Thread(()->lock02(lockM201002)) .start();
//            new Thread(()->lock03(lockM201003)) .start();
//        }
//
//    }
    static int countM201001=1;
    private static void lock01( Lock lock){

            try{
                lock.lock();
                for(int i=0;i<100;i++){
                    countM201001+=1;
                }
                System.out.println("M201001:"+countM201001);
            }finally {
                lock.unlock();
            }
    }

    static int countM201002=1;
    private static void lock02( Lock lock){

        try{
            lock.lock();
            for(int i=0;i<100;i++){
                countM201002+=2;
            }
            System.out.println("M201002:"+countM201002);
        }finally {
            lock.unlock();
        }
    }
    static int countM201003=1;
    private static void lock03( Lock lock){

        try{
            lock.lock();
            for(int i=0;i<100;i++){
                countM201003+=3;
            }
            System.out.println("M201003:"+countM201003);
        }finally {
            lock.unlock();
        }
    }
    public static synchronized  Lock  getLock(String merchantId) {
        //商户号为空，抛出异常
        Assert.notNull(merchantId,"获取商户对象锁时，商户号为 null");
        // 查询商户信息
        Lock lock= merchantsLock.get(merchantId);
        if(null == lock){
            //如果没用锁，则手动创建一把锁，然后放到集合
            lock=new ReentrantLock();
            merchantsLock.put(merchantId,lock);
        }
        return lock;
    }




    public static void haiYiDaiFu_3(){
        for(int i=0;i<1;i++){
            int id=i;
            new Thread(()->{
                try {
                    Map<String, Object> map = new TreeMap<>();
                    map.put("bizType","4");
                    map.put("charset","utf-8");
                    map.put("signType","MD5");
                    map.put("merId","M201003");
                    map.put("merOrderId","20190715000200006"+id);
                    map.put("amount","10");
                    map.put("bankCode","ABC");
                    map.put("inAcctNo","6228480078071578275");
                    map.put("inAcctName","谭廷东");
                    map.put("returnUrl","http://...");
                    map.put("noticeUrl","http://192.168.1.67:8001/cross/inwardPay/testNoticeUrl");
                    map.put("remark","代付测试");                              //lXN7z498    357g4Y4v
                    map.put("signMsg", CheckMd5Utils.getMd5SignWithKey(map,"357g4Y4v"));
                    String tradeInfo = JsonUtils.objectToJson(map);
                    tradeInfo= URLEncoder.encode(tradeInfo,"UTF-8");
                    String postJson= new String( Base64.getEncoder().encode(tradeInfo.getBytes()),"UTF-8");
//                    String content = HttpClientUtils.doPostJson(HttpClientUtils.getHttpsClient(), "http://localhost:8070/inwardPay/payForAnother", postJson);
                    String content = HttpClientUtils.doPostJson(HttpClientUtils.getHttpsClient(), "http://inward.cmtbuy.com/inward/inwardPay/payForAnother", postJson);
//                String content = HttpClientUtils.doPostJson(HttpClientUtils.getHttpsClient(), "http://outpay.xmfusepay.com/inward/payForAnother", postJson);
                    System.out.println(content);
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }).start();
        }
    }


}
